package landun.service.impl;

import landun.common.Constants;
import landun.dao.BizCheckResultDao;
import landun.dao.BizClaimVoucherDao;
import landun.dao.EmpDao;
import landun.pojo.BizCheckResult;
import landun.pojo.BizClaimVoucher;
import landun.service.BizCheckResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("bizCheckResultService")
public class BizCheckResultServiceImpl implements BizCheckResultService {
    @Autowired
    private BizCheckResultDao bizCheckResultDao;
    @Autowired
    private BizClaimVoucherDao bizClaimVoucherDao;
    @Autowired
    private EmpDao empDao;
    @Override
    public List<BizCheckResult> findCheckResult(Integer claimVoucherId) {
        return bizCheckResultDao.findCheckResult(claimVoucherId);
    }


    @Override
    public void save(BizCheckResult checkResult) {
        checkResult.setCheckTime(new Date());
        bizCheckResultDao.save(checkResult);

        BizClaimVoucher claimVoucher = bizClaimVoucherDao.get(checkResult
                .getClaimId());
        claimVoucher.setModifyTime(checkResult.getCheckTime());
        this.updateClaimVoucherStatus(checkResult.getCheckerEmp()
                        .getPosition().getNameCn(), checkResult.getResult(),
                claimVoucher);
    }

    protected void updateClaimVoucherStatus(String position,
                                            String checkResult, BizClaimVoucher claimVoucher) {
        // 通过
        if (Constants.CHECKRESULT_PASS.equals(checkResult)) {
            // FM
            if (Constants.POSITION_FM.equals(position)) {
                // >=5000
                if (claimVoucher.getTotalAccount() >= 5000) {
                    claimVoucher.setStatus("待审批");
                    claimVoucher.setNextDeal(empDao.findByPosition(3,null));
                }
                // < 5000
                else {
                    claimVoucher.setStatus("已审批");
                    claimVoucher.setNextDeal(empDao.findByPosition(4,null));
                }
            }
            // GM
            else if (Constants.POSITION_GM.equals(position)) {
                claimVoucher.setStatus("已审批");
                claimVoucher.setNextDeal(empDao.findByPosition(4,null));
            }
            // Cashier
            else if (Constants.POSITION_CASHIER.equals(position)) {
                claimVoucher.setStatus("已付款");
                claimVoucher.setNextDeal(null);
            }
        }
        // 拒绝
        else if (Constants.CHECKRESULT_REJECT.equals(checkResult)) {
            claimVoucher.setStatus("已终止");
            claimVoucher.setNextDeal(null);
        }
        // 打回
        else if (Constants.CHECKRESULT_BACK.equals(checkResult)) {
            claimVoucher.setStatus("已打回");
            claimVoucher.setNextDeal(claimVoucher.getCreator());
        }
    }

    public BizCheckResultDao getCheckResultDao() {
        return bizCheckResultDao;
    }

    public void setCheckResultDao(BizCheckResultDao checkResultDao) {
        this.bizCheckResultDao = checkResultDao;
    }

    public BizClaimVoucherDao getClaimVoucherDao() {
        return bizClaimVoucherDao;
    }

    public void setClaimVoucherDao(BizClaimVoucherDao claimVoucherDao) {
        this.bizClaimVoucherDao = claimVoucherDao;
    }

    public EmpDao getEmployeeDao() {
        return empDao;
    }

    public void setEmployeeDao(EmpDao employeeDao) {
        this.empDao = employeeDao;
    }
}
